package com.zhongke.cotrun.controller;

import com.github.pagehelper.PageInfo;
import com.zhongke.cotrun.common.annotation.SysLog;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.pojo.InnovationActivitiesEntity;
import com.zhongke.cotrun.model.pojo.InnovationActivitiesEntityVo;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.model.validator.UserValidator;
import com.zhongke.cotrun.service.InnovationActivitiesService;
import com.zhongke.cotrun.shiro.sys.shiro.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;


/**
 * 创新活动表
 *
 * @author lee
 * @email wawzj512541@gmail.com
 * @date 2019-11-15 14:59:00
 */
@Api(tags = "创新活动")
@Validated
@RestController
@RequestMapping("/innovationActivities")
public class InnovationActivitiesController extends AbstractController{

    @Autowired
    private InnovationActivitiesService innovationActivitiesService;
    /**
     * 新增
     * @param innovationActivitiesEntity
     * @return APIResponse
     */
    @ApiOperation(value = "新增创新活动", httpMethod = "POST")
    @SysLog(value = "提交活动",type = "创新活动")
    @PostMapping("/addInnovationActivities")
    @RequiresPermissions("web:activities:save")
    public APIResponse addInnovationActivities(@Validated({UserValidator.InSet.class}) InnovationActivitiesEntity innovationActivitiesEntity){
        innovationActivitiesEntity.setCreateId(ShiroUtils.getWebUserId());
        if(innovationActivitiesService.addInnovationActivities(innovationActivitiesEntity)){
            return APIResponse.returnSuccess("添加成功");
        }else{
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 逻辑删除
     * @param id    主键id
     * @return APIResponse
     */
    @ApiOperation(value = "删除创新活动", httpMethod = "POST")
    @RequiresPermissions("web:activities:delete")
    @SysLog(value = "删除活动",type = "创新活动")
    @RequestMapping(value="/deleteById",method = {RequestMethod.DELETE,RequestMethod.POST})
    public APIResponse deleteById(@NotNull(message = "参数不能为空")@ApiParam(value = "主键id", required = true)Integer id){
        if(innovationActivitiesService.deleteByID(id)){
            return APIResponse.returnSuccess("删除成功");
        }else{
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 批量删除
     * @param ids    主键ids(多个id用英文逗号分隔)
     * @return APIResponse
     */
    @ApiOperation(value = "批量删除", httpMethod = "POST")
    @RequiresPermissions("web:activities:deleteByBatch")
    @SysLog(value = "批量删除活动",type = "创新活动")
    @RequestMapping(value="/deleteByBatch",method = {RequestMethod.DELETE,RequestMethod.POST})
    public APIResponse deleteByBatch(@NotNull(message = "参数不能为空")@ApiParam(value = "主键id,多个id用逗号隔开", required = true)String ids){
        if(innovationActivitiesService.deleteBatch(ids)){
            return APIResponse.returnSuccess("删除成功");
        }else{
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 修改
     * @param innovationActivitiesEntity    实体类
     * @return APIResponse
     */
    @ApiOperation(value = "修改活动", httpMethod = "POST")
    @RequiresPermissions("web:activities:update")
    @SysLog(value = "修改活动",type = "创新活动")
    @PostMapping("/updateById")
    public APIResponse updateById(@Validated({UserValidator.UpDate.class}) InnovationActivitiesEntity innovationActivitiesEntity){
        if(innovationActivitiesService.updateByID(innovationActivitiesEntity)){
            return APIResponse.returnSuccess("修改成功");
        }else{
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 修改状态
     */
    @ApiOperation(value = "修改状态", httpMethod = "POST")
    @RequiresPermissions("web:activities:update")
    @SysLog(value = "修改活动状态",type = "创新活动")
    @PostMapping("updateStatus")
    public APIResponse updateStatus(@NotNull(message = "参数不能为空")@ApiParam(value = "主键id", required = true)Integer id,
                                    @NotNull(message = "状态不能为空")@ApiParam(value = "状态", required = true)Integer status){
        if(innovationActivitiesService.updateStatus(id,status)){
            return APIResponse.returnSuccess("操作成功");
        }else{
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 根据id搜索
     * @param id    主键id
     * @return APIResponse
     */
    @ApiOperation(value = "根据id搜索详情(修改时使用)", httpMethod = "GET")
    @RequiresPermissions("web:activities:info")
    @RequestMapping(value="/getById",method = RequestMethod.GET)
    public APIResponse getById(@NotNull(message = "参数不能为空")@ApiParam(value = "主键id", required = true)Integer id){
        InnovationActivitiesEntityVo obj = innovationActivitiesService.selectByID(id);
        if(obj!=null){
            return APIResponse.returnSuccess(obj);
        }else{
            return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
        }
    }

    /**
     * 分页展示,可根据条件筛选
     * @param innovationActivitiesEntity    实体类
     * @param pageNum   页码
     * @param pageSize  页面尺寸
     * @return APIResponse
     */
    @ApiOperation(value = "分页展示", httpMethod = "GET")
    @RequestMapping(value="/showByPage",method = RequestMethod.GET)
    public APIResponse showByPage(InnovationActivitiesEntityVo innovationActivitiesEntity,
                                  @ApiParam(value = "页码", required = true)@RequestParam(defaultValue = "1") Integer pageNum,
                                  @ApiParam(value = "尺寸", required = true)@RequestParam(defaultValue = "10")Integer pageSize){
        innovationActivitiesEntity.setApprovalStatus(1);
        PageInfo pageInfo = innovationActivitiesService.showByPage(innovationActivitiesEntity,pageNum,pageSize);
        return APIResponse.returnSuccess(pageInfo);
    }

    /**
     * 个人中心分页展示,可根据条件筛选
     * @param innovationActivitiesEntity    实体类
     * @param pageNum   页码
     * @param pageSize  页面尺寸
     * @return APIResponse
     */
    @ApiOperation(value = "分页展示-个人中心", httpMethod = "GET")
    @RequiresPermissions("web:activities:list")
    @RequestMapping(value="/showByPageForCenter",method = RequestMethod.GET)
    public APIResponse showByPageForCenter(InnovationActivitiesEntityVo innovationActivitiesEntity,
                                           @ApiParam(value = "页码", required = true)@RequestParam(defaultValue = "1") Integer pageNum,
                                           @ApiParam(value = "尺寸", required = true)@RequestParam(defaultValue = "10")Integer pageSize){

        PageInfo pageInfo = innovationActivitiesService.showByPageForCenter(innovationActivitiesEntity,pageNum,pageSize);
        return APIResponse.returnSuccess(pageInfo);
    }

    /**
     * 我参与的活动
     * @param innovationActivitiesEntity    实体类
     * @param pageNum   页码
     * @param pageSize  页面尺寸
     * @return APIResponse
     */
    @ApiOperation(value = "我参与的活动", httpMethod = "GET")
    @RequestMapping(value="/queryOrderForCenter",method = RequestMethod.GET)
    public APIResponse queryOrderForCenter(InnovationActivitiesEntityVo innovationActivitiesEntity,
                                           @ApiParam(value = "页码", required = true)@RequestParam(defaultValue = "1") Integer pageNum,
                                           @ApiParam(value = "尺寸", required = true)@RequestParam(defaultValue = "10")Integer pageSize){
        PageInfo pageInfo = innovationActivitiesService.queryOrderForCenter(innovationActivitiesEntity,pageNum,pageSize);
        return APIResponse.returnSuccess(pageInfo);
    }

    /**
     * 根据id显示详情
     * @param id    主键id
     * @return APIResponse
     */
    @ApiOperation(value = "根据id显示详情", httpMethod = "GET")
    @RequestMapping(value="/showById",method = RequestMethod.GET)
    public APIResponse showById(@NotNull(message = "参数不能为空")@ApiParam(value = "主键id", required = true)Integer id){
        InnovationActivitiesEntityVo obj = innovationActivitiesService.selectByID(id);
        if(obj!=null){
            return APIResponse.returnSuccess(obj);
        }else{
            return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
        }
    }

    /**
     * 下线活动
     *
     * @param id    主键id
     * @return APIResponse
     */
    @ApiOperation(value = "下线活动", httpMethod = "POST")
    @RequiresPermissions("web:activities:cancel")
    @SysLog(value="下线活动",type="创新活动")
    @PostMapping("/closeActivities")
    public APIResponse closeActivities(@NotNull(message = "参数不能为空") @ApiParam(value = "主键id", required = true)Integer id) {
        if (innovationActivitiesService.closeActivities(id)) {
            return APIResponse.returnSuccess();
        } else {
            return APIResponse.returnFail(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * 点击报名时获取活动信息
     * @param id
     * @return
     */
    @ApiOperation(value = "点击报名时获取活动信息", httpMethod = "GET")
    @GetMapping("/getSignUpInfo")
    public APIResponse signUpInfo(Integer id){
        InnovationActivitiesEntityVo entityVo = innovationActivitiesService.getSignUpInfo(id);
        if(entityVo!=null){
            return APIResponse.returnSuccess(entityVo);
        }
        return APIResponse.returnFail(CodeMsg.DATA_EMPTY);
    }
}
